package td.web.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import td.ejb.users.UserDTO;
import td.ejb.users.UserMgr;

@ManagedBean(name="userBean")
@RequestScoped
public class UserBean {
	
	@EJB
	private UserMgr userMgr;
	
	private UserDTO utente;

	public UserDTO getUtente() {
		return utente;
	}

	public void setUtente(UserDTO utente) {
		this.utente = utente;
	}
	
	public String getUsername() {
		return userMgr.getUserDTO().getUsername();
	}
	
	public String getNome() {
		return userMgr.getUserDTO().getNome();
	}
	
	public String getCognome() {
		return userMgr.getUserDTO().getCognome();
	}
	
	public String getEmail() {
		return userMgr.getUserDTO().getEmail();
	}
	
	// _____________________LISTA
	
	private int numAgent;
	
	public List<UserDTO> listaAgent() {
		List<UserDTO> lista = userMgr.listaAgent();
		numAgent = lista.size();
		return lista;
	}
	
	public int getNumAgent() {
		return numAgent;
	}

	public void setNumAgent(int numAgent) {
		this.numAgent = numAgent;
	}
	
	// _____________________RIMUOVI

	public void cancellaUtente(ActionEvent actionEvent) {
		userMgr.unregister(utente.getUsername());
		FacesContext.getCurrentInstance().addMessage("regSubmit", new FacesMessage(FacesMessage.SEVERITY_WARN,"Rimosso Travel Agent: ", utente.getUsername()));  
	}
	
	// _____________________FILTRO

	private List<UserDTO> filteredAgent;

	public List<UserDTO> getFilteredAgent() {
		return filteredAgent;
	}
	
	public void setFilteredAgent(List<UserDTO> filteredAgent) {
		this.filteredAgent = filteredAgent;
	}
}
